package lib.bone.flow.service.impl;

import cn.hutool.core.lang.Validator;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.flow.dao.FlowAttachDAO;
import lib.bone.flow.domain.entity.FlowAttach;
import lib.bone.flow.service.FlowAttachService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class FlowAttachServiceImpl implements FlowAttachService {
    @Autowired
    FlowAttachDAO dao;

    @Override
    @Transactional
    public void deleteByFlow(String flowWorkId) {
        dao.deleteByFlowWorkId(flowWorkId);
    }

    @Override
    public FlowAttach saveOne(FlowAttach attach) {
        return dao.save(attach);
    }

    @Override
    public Optional<FlowAttach> findById(String id) {
        return dao.findById(id);
    }

    @Override
    public ResponseDTO getList(Pageable pageable, Example<FlowAttach> example) {
        Page<FlowAttach> page;
        //是否为空 --
        if(Validator.isEmpty(example)){
            page = dao.findAll(pageable);
        }else {
            page = dao.findAll(example,pageable);
        }
        List<FlowAttach> list = page.getContent();
        PageResultDTO<FlowAttach> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());
        return ResponseDTO.succData(pageResultDTO);
    }
}
